var util = require('../../utils/util.js');
var ajax = require('../../utils/ajax.js');
var common = require('../../utils/common.js');
var PageBase = require('../../common/PageBase.js');
var area = require('../../utils/area.js');
var app = getApp();

Page(common.extendPage(PageBase, {
  data: {
    tabs: ['类别', '区域', '好评优先'],
    isShow: false,
    currentTab: 0,
    currentRegion: 0,
    typeList: [],
    regionList: [],
    shopList: [],
    serType: '',
    sortStart: 0,
    city: '',
    region: '',
    loading: true
  },
  /**
   * 页面初始化
   */
  onLoad(options) {
    this._options = options;
    const isShow = this._options.cateType != 2 ? 1 : 0;
    var regionList = this.data.regionList;
    this._loadingNum = 2;
    area.init(this);
    this.setData({
      isShow,
      currentTab: this._options.cateType,
      city: wx.getStorageSync('city') || wx.getStorageSync('locCity')
    })
    regionList.push(this.data.city);
    this.data.areaData.forEach((item, index) => {
      item.sub.forEach((val, idx) => {
        if (val.name == this.data.city) {
          console.log(val.code);
          this.getArea(val.code);
        }
      })
    })
    this.getTabs();
    this.flush(true);
  },
  /**
   *下拉加载更多
   */
  onReachBottom() {
    this.loadMore()
  },
  /**
   * 切换tab
   */
  _switchTab(evt) {
    const {
      currentTab
    } = evt.currentTarget.dataset;
    const isShow = currentTab != 2 ? 1 : 0;
    let serType = this.data.serType;
    const typeList = this.data.typeList;
    const {
      currentRegion,
      regionList
    } = this.data;

    if(currentTab == 0){
      typeList.some((item, index) => {
        if (item.isSelected) {
          serType = item.name;
        }
      })
    }else if(currentTab == 1){
      serType = regionList[currentRegion]
    }

    this.setData({
      currentTab,
      isShow,
      sortStart: currentTab == 2 ? 1 : 0,
      serType
    })
    if (currentTab == 2){
      this.flush();
    }
  },
  /**
   * 类别选项切换
   */
  _switchType(evt) {
    var typeList = this.data.typeList;
    var currentType = evt.currentTarget.dataset.currentType;
    var serType = this.data.serType;
    typeList.some((item, index) => {
      item.isSelected = false;
      if (currentType == index) {
        item.isSelected = true;
        serType = item.name;
      }
    })

    this.setData({
      typeList,
      serType
    })

  },
  /**
   * 选中类别提交
   */
  _submit() {
    this._closed(null, () => {
      this.flush()
    });
  },
  /**
   * 关闭模态框
   */
  _closed(evt, cb) {
    this.setData({
      isShow: false
    })
    cb && cb();
  },
  /**
   *刷新
   */
  flush(isInit) {
    this.setData({
      page: 1,
      allLoaded: false,
      noMore: false
    });
    this.getShopList(true, false, () => {
      wx.stopPullDownRefresh();
      if (isInit) {
        this._loadingNum--;
        if (this._loadingNum == 0) {
          this.setData({
            loading: false
          })
        }
      }
    });
  },
  /**
   * 获取店铺列表
   */
  getShopList(isFlush, unneedLoad, cb) {
    var page = this.data.page;
    var shopList = this.data.shopList;
    var region = this.data.region;
    var paramsUrl = '/public/api/shops/shoplist';
    var paramsData = {
      types: this._options.type,
      sertype: this.data.serType || '',
      sort_start: this.data.sortStart,
      page: page || 1,
      num: 10
    };

    ajax.P({
      url: paramsUrl,
      data: paramsData,
      success: (res, isLoadcb) => {
        var listData = res.data || [];
        var allLoaded = false;
        var noMore = false;
        if (listData.length == 0) {
          allLoaded = true;
          noMore = true;
        }
        if (isFlush) {
          shopList = listData;
        } else {
          shopList = shopList.concat(listData);
        }

        this.setData({
          shopList,
          page: ++page,
          allLoaded,
          noMore
        })
        isLoadcb && isLoadcb();
        cb && cb();
      },
      fail: () => {},
      unneedLoad
    })
  },
  /**
   * 加载更多
   */
  loadMore() {
    var {
      allLoaded
    } = this.data;

    if (allLoaded) {
      return;
    } else {
      this.setData({
        isLoadMore: true
      })
      this.getShopList(false, true, () => {
        this.setData({
          dataLoaded: true,
          isLoadMore: false
        })
      });
    }
  },
  /**
   * 获取类别项
   */
  getTabs() {
    var paramsUrl = '/public/api/shops/storeService';
    var paramsData = {
      types: this._options.type
    }
    ajax.P({
      url: paramsUrl,
      data: paramsData,
      success: (res, isLoadcb) => {
        var title = res.data.title;
        var typeList = this.data.typeList;
        this._loadingNum--;
        if (this._loadingNum == 0) {
          this._loading = false;
        }
        title.forEach((item, index) => {
          var typeItem = {};
          typeItem.name = item;
          typeItem.isSelected = false;
          typeList.push(typeItem);
        })
        typeList[0].isSelected = true;
        this.setData({
          typeList,
          loading: this._loading
        })
        isLoadcb && isLoadcb();
      }
    })
  },
  /**
   * 切换地理位置
   */
  _switchRegion(evt) {
    var {
      currentIndex
    } = evt.currentTarget.dataset;
    var {
      currentRegion,
      regionList
    } = this.data;

    this.setData({
      region: regionList[currentIndex],
      currentRegion: currentIndex,
      serType: regionList[currentIndex]
    })
    this._closed(null, () => {
      this.flush()
    });
  },
  getArea(code){
   
    const paramsUrl = '/public/api/common/citydata';
    ajax.P({
      url:paramsUrl,
      data:{
        code
      },
      success:(res)=>{
        console.log('*****************')
        let regionList = [];
        res.forEach((item,index)=>{
          regionList.push(item.address);
        })
        this.setData({
          regionList
        })
      }
    })
  }
}));